<?php

class ArticleController extends MyController
{
	function init() {parent::init(); }
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($acategory_id = 0, $page = 1)
	{
		$pages = new CPagination();
		$pages->pageSize = $this->website->beperpage;
		
		$rows = NULL;
		
		if ($acategory_id > 0)
		{
			//$acategory = acategory::model()->findByPk($acategory_id);
			
			$count_subcat = acategory::model()->count(array(
				"condition" => "parent_id = " . $acategory_id
			));
			
			if ($count_subcat == 0 )
			{
				$rows = article::model()->findAll(
					array(
						"condition"=>"trash = 0 AND acategory_id = " . $acategory_id,
						"order" => "id DESC,`order` ASC, create_time DESC",
						"offset" => ($page-1)*$this->website->beperpage,
						"limit" => $this->website->beperpage
					)
				);
				
				$pages->itemCount = article::model()->count(array(
					"condition"=>"trash = 0 AND acategory_id = " . $acategory_id
				));
				
			}
			else
			{
				$rows = article::model()->findAll(
					array(
						"condition"=>"trash = 0 AND ( acategory_id = " . $acategory_id . 
							" OR acategory_id IN 
								(SELECT id FROM acategory WHERE parent_id = ".$acategory_id."))",
						"order" => "id DESC,`order` ASC, create_time DESC",
						"offset" => ($page-1)*$this->website->beperpage,
						"limit" => $this->website->beperpage
					)
				);
				
				$pages->itemCount = article::model()->count(array(
					"condition"=>"trash = 0 AND ( acategory_id = " . $acategory_id . 
							" OR acategory_id IN 
								(SELECT id FROM acategory WHERE parent_id = ".$acategory_id."))"
				));
			}
			
		}
		else
		{
			$rows = article::model()->findAll(array(
				"condition"=>"trash = 0",
				"order" => "id DESC,`order` ASC, create_time DESC, id ASC",
				"offset" => ($page-1) * $this->website->beperpage,
				"limit" => $this->website->beperpage
			));
			
			$pages->itemCount = article::model()->count(
				array("condition"=>"trash = 0")
			);
			
		}
		
		$this->data['rows']= $rows;	
		$this->data['acategory_id']= $acategory_id;	
		$this->data['pages']= $pages;
		$this->data["page"] = $page;
		
		$this->render('view', $this->data);
		
	}
	
	function actionAdd($acategory_id = 0, $page = 1)
	{
		$row = new article;
		$row->id = 0;
		$row->create_time = MyHelper::get_datetime();
		$row->start_date = MyHelper::get_date();
		$row->end_date = MyHelper::get_date();
		
		$row->allow_show_summary = 1;
		
		$this->data["row"] = $row;
		
		$this->data['acategory_id']= $acategory_id;	
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$row = article::model()->findByPk($article_id);

		$this->data["row"] = $row;		
		$this->data['acategory_id']= $acategory_id;	
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$article = NULL;
		if ($article_id == 0)
		{
			$row = new article;
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->create_user_id = $this->user->id;
			$row->enable = 1;
			$row->trash = 0;
			
			$row->save();
			
		}else{
			$row = article::model()->findByPk($article_id);
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
	
			$row->update_user_id = $this->user->id;
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	
	function actionSavenew($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$article = NULL;
		if ($article_id == 0)
		{
			$row = new article;
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->create_user_id = $this->user->id;
			$row->enable = 1;
			$row->trash = 0;
			
			$row->save();
			
		}else{
			$row = article::model()->findByPk($article_id);
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
	
			$row->update_user_id = $this->user->id;
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/add', 
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	
	function actionApply($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$article = NULL;
		if ($article_id == 0)
		{
			$row = new article;
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->create_user_id = $this->user->id;
			$row->enable = 1;
			$row->trash = 0;
			
			$row->save();
			
		}else{
			$row = article::model()->findByPk($article_id);
			$row->setAttributes($_POST, false);
			
			$url = str_replace("/".ADMIN_FOLDER,"", Yii::app()->baseUrl);
			$row->content = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("content")
			);
			
			$row->image = str_replace(
				"../upload/",
				$url."/upload/",
				Yii::app()->getRequest()->getPost("image")
			);
	
			$row->update_user_id = $this->user->id;
			$row->alias = strtolower(mii::clear_sign($row->subject));
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/edit', "article_id" => $article_id,
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	function actionEnable($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$row = article::model()->findByPk($article_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		
		if ($row->trash == 0)
			$this->redirect(array($this->controller_id.'/view', 
				"acategory_id"=>$acategory_id, "page" => $page));
		else
			$this->redirect(array($this->controller_id.'/view_trash', 
				"acategory_id"=>$acategory_id, "page" => $page));
	}
	function actionCopy($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$row = article::model()->findByPk($article_id);
		$row_copy = new article;
		
		$row_copy->save();
		
		$this->redirect(array($this->controller_id.'/view', 
				"acategory_id"=>$acategory_id, "page" => $page));
	}

	function actionDelete($article_id = 0, $acategory_id = 0, $page = 1)
	{	
		// move article into the trash
		$row = article::model()->findByPk($article_id);
		$row->trash = 1;
		$row->enable = 0;
		$row->save();
		
		$rows = NULL;
		
		if ($acategory_id > 0)
		{
			$rows = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 0",
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage
			));
				
		}
		else
		{
			$rows = article::model()->findAll(array(
				"condition"=>"trash = 0",
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"acategory_id"=>$acategory_id, "page" => $page));	
	}
	
	function actionView_trash($acategory_id = 0, $page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->data["website"]["beperpage"];
		
		$articles = NULL;
		if ($acategory_id > 0)
		{
			$articles = article::model()->findAll(
				array(
					"condition"=>"trash = 1 AND acategory_id = " . $acategory_id,
					"order" => "article_order ASC",
					"offset" => ($page-1)*$this->data["website"]["beperpage"],
					"limit" => $this->data["website"]["beperpage"]
				)
			);
			
			$pages->itemCount =  article::model()->count(array(
				"condition"=>"trash = 1 AND acategory_id = " . $acategory_id
			));
		}
		else
		{
			
			$articles = article::model()->findAll(array(
				"condition"=>"trash = 1",
				"order" => "article_order ASC, id ASC",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit" => $this->data["website"]["beperpage"]
			));
			
			$itemCount = article::model()->count(array("condition"=>"trash = 1"));
			$pages->itemCount = $itemCount; // tong so dong
			
		}
		
		$this->data['articles']= $articles;	
		$this->data['acategory_id']= $acategory_id;	
		
		$this->data["pages"] = $pages;
		$this->data["page"] = $page;
		
		$this->render('view_trash', $this->data);
	}
	
	
	function actionConfirm_delete_trash($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$article = article::model()->findByPk($article_id);
		$this->data['article'] = $article;
		$this->data['acategory_id'] = $acategory_id;
		
		$this->data['page'] = $page;
		
		$this->render("confirm_delete_trash", $this->data);
	}
	
	function actionDelete_trash($article_id = 0, $acategory_id = 0, $page = 1)
	{
		article::model()->deleteByPk($article_id);
		
		$articles = NULL;
		if ($acategory_id > 0)
		{
			// bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit" => $this->data["website"]["beperpage"]
			));
				
		}
		else
		{
			// bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit"	 => $this->data["website"]["beperpage"]
			));	
		}
		
		if (count($articles) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/view_trash', 
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	
	function actionRestore($article_id = 0, $acategory_id = 0, $page = 1)
	{
		$article = article::model()->findByPk($article_id);
		$article->trash = 0;
		$article->save();
		
		$articles = NULL;
		if ($acategory_id > 0)
		{
			// bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit" => $this->data["website"]["beperpage"]
			));
				
		}
		else
		{
			//bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit"	 => $this->data["website"]["beperpage"]
			));	
		}
		
		if (count($articles) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/view_trash',
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	
	function actionRestore_multi( $acategory_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$article = article::model()->findByPk($id);
				$article->trash = 0;
				$article->save();
			}
		}
		
		$articles = NULL;
		if ($acategory_id > 0)
		{
			// bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit" => $this->data["website"]["beperpage"]
			));
				
		}
		else
		{
			// bai viet trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit"	 => $this->data["website"]["beperpage"]
			));	
		}
		
		if (count($articles) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/view_trash', 
			"acategory_id"=>$acategory_id, "page"=>$page));
	}
	
	// giam article_order cua article 1 don vi
	function actionUp($article_id = 0, $acategory_id = 0)
	{
		$article = article::model()->findByPk($article_id);
		
		if ($article->article_order > 1)
		{
			$article_previous = article::model()->find(array(
				"condition" => "acategory_id = " . $article->acategory_id . 
								" AND article_order = " . ($article->article_order - 1)
			));
			
			if (! is_null($article_previous))
			{
				$article_previous->article_order ++;
				$article_previous->save();
			}
			
			$article->article_order --;
			$article->save();
		}
		
		
		$this->redirect(array($this->data['controller_id'].'/view', 
				"acategory_id"=>$acategory_id));
	}
	
	// tang article_order cua article 1 don vi
	function actionDown($article_id = 0, $acategory_id = 0)
	{
		$article = article::model()->findByPk($article_id);
		
		$max_article_order = Yii::app()->db->createCommand(
						"SELECT max(`article_order`) " . 
						"FROM article WHERE acategory_id=" . $article->acategory_id )
						->queryScalar();
						
		if ($article->article_order < $max_article_order)
		{
			$article_next = article::model()->find(array(
				"condition" => "acategory_id = " . $article->acategory_id . 
								" AND article_order = " . ($article->article_order + 1)
			));
			
			if (! is_null($article_next))
			{
				$article_next->article_order --;
				$article_next->save();
			}
			
			$article->article_order ++;
			$article->save();
		}
		
		
		$this->redirect(array($this->data['controller_id'].'/view', 
				"acategory_id"=>$acategory_id));
	}
	
	function actionDelete_multi($acategory_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$row = article::model()->findByPk($id);
				$row->trash = 1;
				$row->save();
			}
		}
		
		$rows = NULL;
		if ($acategory_id > 0)
		{
			$rows = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 0",
				"offset" => ($page-1)*$this->website->beperpage,
				"limit" => $this->website->beperpage,
			));
				
		}
		else
		{
			$rows = article::model()->findAll(array(
				"condition"=>"trash = 0",
				"offset" => ($page-1)*$this->website->beperpage,
				"limit"	 => $this->website->beperpage,
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"acategory_id"=>$acategory_id, "page"=>$page));
	}
	
	function actionDelete_multi_trash( $acategory_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$article = article::model()->findByPk($id);
				$article->delete();
			}
		}
		
		$articles = NULL;
		if ($acategory_id > 0)
		{
			// mon an cua trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"acategory_id = " . $acategory_id. " AND trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit" => $this->data["website"]["beperpage"]
			));
				
		}
		else
		{
			// mon an cua trang hien tai
			$articles = article::model()->findAll(array(
				"condition"=>"trash = 1",
				"offset" => ($page-1)*$this->data["website"]["beperpage"],
				"limit"	 => $this->data["website"]["beperpage"]
			));	
		}
		
		if (count($articles) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/view_trash', 
			"acategory_id"=>$acategory_id, "page" => $page));
	}
	
	
}